package com.hb.hb_case;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import com.core.*;
import com.wonders.ips.common.DbConnection;

/**
 * Data access object (DAO) for domain model class HbCase.
 * @see com.hb.hb_case.HbCase
 * @author MyEclipse - Hibernate Tools
 */
public class HbCaseDAO extends BaseHibernateDAO {

    private static final Log log = LogFactory.getLog(HbCaseDAO.class);

	//property constants
	public static final String USERID = "userid";
	public static final String NEEDID = "needid";
	public static final String NAME = "name";
	public static final String TYPE = "type";
	public static final String URL = "url";
	public static final String CONTENT = "content";
	public static final String MEMO = "memo";
	public static final String CVIEW = "cview";
	public static final String CVOTE = "cvote";
	public static final String PUB_FLAG = "pubFlag";

	Transaction tx=null;
    
    public void save(HbCase transientInstance) {
        log.debug("saving HbCase instance");
        try {
        	tx=getSession().getTransaction();
        	tx.begin();
            getSession().save(transientInstance);
            tx.commit();
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    
	public void delete(HbCase persistentInstance) {
        log.debug("deleting HbCase instance");
        try {
        	tx=getSession().getTransaction();
        	tx.begin();
            getSession().delete(persistentInstance);
            tx.commit();
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
    
    public HbCase findById( java.lang.Integer id) {
        log.debug("getting HbCase instance with id: " + id);
        try {
        	tx=getSession().getTransaction();
        	tx.begin();
            HbCase instance = (HbCase) getSession()
                    .get("com.hb.hb_case.HbCase", id);
            tx.commit();
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    
    public List findByExample(HbCase instance) {
        log.debug("finding HbCase instance by example");
        try {
        	tx=getSession().getTransaction();
        	tx.begin();
            List results = getSession()
                    .createCriteria("com.hb.hb_case.HbCase")
                    .add(Example.create(instance))
            .list();
            tx.commit();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding HbCase instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from HbCase as model where model." 
         						+ propertyName + "= ?";
         tx=getSession().getTransaction();
     	tx.begin();
         Query queryObject = getSession().createQuery(queryString);
		 queryObject.setParameter(0, value);
		 tx.commit();
		 return queryObject.list();
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

	public List findByUserid(Object userid) {
		return findByProperty(USERID, userid);
	}
	
	public List findByNeedid(Object needid) {
		return findByProperty(NEEDID, needid);
	}
	
	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}
	
	public List findByType(Object type) {
		return findByProperty(TYPE, type);
	}
	
	public List findByUrl(Object url) {
		return findByProperty(URL, url);
	}
	
	public List findByContent(Object content) {
		return findByProperty(CONTENT, content);
	}
	
	public List findByMemo(Object memo) {
		return findByProperty(MEMO, memo);
	}
	
	public List findByCview(Object cview) {
		return findByProperty(CVIEW, cview);
	}
	
	public List findByCvote(Object cvote) {
		return findByProperty(CVOTE, cvote);
	}
	
	public List findByPubFlag(Object pubFlag) {
		return findByProperty(PUB_FLAG, pubFlag);
	}
	
    public HbCase merge(HbCase detachedInstance) {
        log.debug("merging HbCase instance");
        try {
        	tx=getSession().getTransaction();
        	tx.begin();
            HbCase result = (HbCase) getSession()
                    .merge(detachedInstance);
            tx.commit();
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(HbCase instance) {
        log.debug("attaching dirty HbCase instance");
        try {
        	tx=getSession().getTransaction();
        	tx.begin();
            getSession().saveOrUpdate(instance);
            tx.commit();
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(HbCase instance) {
        log.debug("attaching clean HbCase instance");
        try {
        	tx=getSession().getTransaction();
        	tx.begin();
            getSession().lock(instance, LockMode.NONE);
            tx.commit();
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    public void pub(int id)
    throws SQLException
{
    DbConnection dbcon = null;
    Connection conn = null;
    PreparedStatement pstmt = null;
    String sql="";
    try
    {
        dbcon = new DbConnection();
        conn = dbcon.getCon();
        sql = "update hb_case set pub_flag=1 and  pubdate=(select sysdate()) where id=" + id;
        pstmt = conn.prepareStatement(sql);
        pstmt.executeUpdate();
    }
    catch(Exception e)
    {
        System.err.println(e.getMessage());
    }
    finally
    {
        pstmt.close();
        conn.close();
    }
    return;
}
    public int getMaxid()
    throws SQLException
{
    DbConnection dbcon = null;
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    String sql = "";
    int userid=0;
    try
    {
        dbcon = new DbConnection();
        conn = dbcon.getCon();
        stmt = conn.createStatement();
        sql = "select max(id) from hb_case  ";
        System.out.println("sql=" + sql);
        rs = stmt.executeQuery(sql);
        if(rs.next()){
	        userid=rs.getInt(1);
	        return userid;
	        }
	        else{
	        	return 0;
	        }
        
    }
    catch(Exception e)
    {
        System.err.println(e.getMessage());
        return 0;
    }
    finally
    {
        rs.close();
        stmt.close();
        conn.close();
    }   
}
    public void read(int id)
    throws SQLException
{
    DbConnection dbcon = null;
    Connection conn = null;
    PreparedStatement pstmt = null;
    String sql="";
    try
    {
        dbcon = new DbConnection();
        conn = dbcon.getCon();
        sql = "update hb_case set cview=cview+1  where id=" + id;
        pstmt = conn.prepareStatement(sql);
        pstmt.executeUpdate();
    }
    catch(Exception e)
    {
        System.err.println(e.getMessage());
    }
    finally
    {
        pstmt.close();
        conn.close();
    }
    return;
}
    public void vote(int id)
    throws SQLException
{
    DbConnection dbcon = null;
    Connection conn = null;
    PreparedStatement pstmt = null;
    String sql="";
    try
    {
        dbcon = new DbConnection();
        conn = dbcon.getCon();
        sql = "update hb_case set cvote=cvote+1  where id=" + id;
        pstmt = conn.prepareStatement(sql);
        pstmt.executeUpdate();
    }
    catch(Exception e)
    {
        System.err.println(e.getMessage());
    }
    finally
    {
        pstmt.close();
        conn.close();
    }
    return;
}
}